package web

import (
	"github.com/gin-gonic/gin"
	"go-admin/server/internal/domain"
	"go-admin/server/internal/service"
	"go-admin/server/internal/web/form"
	"net/http"
)

var _ handler = &SettingHandler{}

type SettingHandler struct {
	svc service.SettingService
}

func NewSettingHandler(svc service.SettingService) *SettingHandler {
	return &SettingHandler{
		svc: svc,
	}
}

func (s *SettingHandler) RegisterRoutes(server *gin.Engine) {
	sg := server.Group("/setting")

	sg.PUT("/edit", s.edit)
	sg.GET("/info", s.info)
}

// edit 添加 or 编辑 配置
func (s *SettingHandler) edit(ctx *gin.Context) {
	var rep form.SettingReq
	err := ctx.Bind(&rep)
	if err != nil {
		return
	}
	err = s.svc.Save(ctx.Request.Context(), domain.Setting{
		AliyunSecretId:   rep.AliyunSecretId,
		AliyunSecretKey:  rep.AliyunSecretKey,
		AliyunSignaName:  rep.AliyunSignaName,
		TencentSecretId:  rep.TencentSecretId,
		TencentSecretKey: rep.TencentSecretKey,
		TencentSignaName: rep.TencentSignaName,
		TencentAppId:     rep.TencentAppId,
	})
	if err != nil {
		RenderJSON(ctx, http.StatusOK, http.StatusNoContent, "操作失败", nil)
	}
	RenderJSON(ctx, http.StatusOK, http.StatusOK, "操作成功", nil)
	return
}

// info 获取配置信息
func (s *SettingHandler) info(ctx *gin.Context) {
	info := s.svc.GetInfo(ctx.Request.Context())
	RenderJSON(ctx, http.StatusOK, http.StatusOK, "ok", info)
}
